package cn.com.xinjinfu.pdoulcProduct.fragment.AboutPDou;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.SalfControlInfoAct;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.AboutPdouBean;
import cn.com.xinjinfu.pdoulcProduct.entity.AboutPdouBean.MyPdouMap.Safetys;
import cn.com.xinjinfu.pdoulcProduct.entity.CommonProblemBean;
import cn.com.xinjinfu.pdoulcProduct.entity.CommonProblemBean.MyPdouMap.Lessons;
import cn.com.xinjinfu.pdoulcProduct.entity.CommonProblemBean.MyPdouMap.Questions;
import cn.com.xinjinfu.pdoulcProduct.fragment.BaseFragment;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;
import cn.com.xinjinfu.pdoulcProduct.view.ListViewForScrollView;

/**
 * 关于P兜常见问题
 * Created by android-adt on 2015/12/31.
 */
public class CommonProblemFragment extends Fragment {
    private View viewFragment;
    private Activity mActivity;
    private RequestQueue requen;
    private ListViewForScrollView my_listview1;
    private ListViewForScrollView my_listview2;
    private MyAdapter1 myAdapter1;
    private MyAdapter2 myAdapter2;
    private CommonProblemBean problem;
    private ArrayList<Lessons> lessons;
    private ArrayList<Questions> questions;
    private TextView tv_tel;
    private ScrollView sv_include;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewFragment = inflater.inflate(R.layout.fragment_problems, null);
        return viewFragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        /**************禁止ScrollView自动滑动到底部*********************/
        sv_include = (ScrollView) view.findViewById(R.id.sv_include);
        disableAutoScrollToBottom();
        intiView(view);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requen = Volley.newRequestQueue(mActivity);
        loginRequest();
        if (SharedPreferencesUtils.getUserInfo(mActivity, "problemBean") != "") {
            problem = JSON.parseObject(SharedPreferencesUtils.getUserInfo(mActivity, "problemBean"), CommonProblemBean.class);
            if (problem != null) {
                initData();
            }
        }
    }

    public void intiView(View view){
        tv_tel = (TextView) view.findViewById(R.id.tv_tel);
        my_listview1 = (ListViewForScrollView) view.findViewById(R.id.my_listview1);
        my_listview2 = (ListViewForScrollView) view.findViewById(R.id.my_listview2);
        myAdapter1 = new MyAdapter1(mActivity);
        myAdapter2 = new MyAdapter2(mActivity);
        my_listview1.setAdapter(myAdapter1);
        my_listview2.setAdapter(myAdapter2);
        my_listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, SalfControlInfoAct.class);
                Bundle bundle = new Bundle();
                bundle.putString("adress", "problem");
                bundle.putString("url", questions.get(position).getLinkUrl());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        my_listview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, SalfControlInfoAct.class);
                Bundle bundle = new Bundle();
                bundle.putString("adress", "problem");
                bundle.putString("url", lessons.get(position).getLinkUrl());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }



    protected void initData() {
        lessons = problem.getMyPdouMap().getLessons();
        questions = problem.getMyPdouMap().getQuestions();
        if (lessons!=null&&questions!=null){
            myAdapter1.setList(questions);
            myAdapter2.setList(lessons);
            myAdapter1.notifyDataSetChanged();
            myAdapter2.notifyDataSetChanged();
        }
        tv_tel.setText("咨询热线：400-859-9090");
    }
    /**
     * 网络请求
     */
    private void loginRequest() {
        final Map<String, String> parameters = DataHandler.getParameters("1419");
        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, null);
        requen.add(request);
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i(jobj.toString());
            SharedPreferencesUtils.saveUserInfo(mActivity, "problemBean", jobj.toString());
            if (JSONManager.getError(jobj)==-1) {
                problem = JSON.parseObject(jobj.toString(), CommonProblemBean.class);
                if (problem != null) {
                    initData(); }
            }
        }
    };



    class MyAdapter1 extends BaseAdapter{
        private Activity context;
        private ArrayList<Questions> list;
        private LayoutInflater inflater;

        public void setList(ArrayList<Questions> titles) {
            this.list = titles;
        }
        public MyAdapter1(Activity context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list == null ? 0 : list.size();
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView (int position , View convertView , ViewGroup parent)
        {

            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_problem_list, null);
                holder.text2 = (TextView) convertView.findViewById(R.id.textView2);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Questions questions = list.get(position);
            if (questions != null){
                holder.text2.setText(questions.getTitle());
            }
            return convertView;
        }

        class ViewHolder
        {
            public TextView text2;
        }
    }
    class MyAdapter2 extends BaseAdapter{
        private Activity context;
        private ArrayList<Lessons> list;
        private LayoutInflater inflater;

        public void setList(ArrayList<Lessons> titles) {
            this.list = titles;
        }
        public MyAdapter2(Activity context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list == null ? 0 : list.size();
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView (int position , View convertView , ViewGroup parent)
        {

            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_problem_list2, null);
                holder.text2 = (TextView) convertView.findViewById(R.id.textView2);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Lessons lessons = list.get(position);
            if (lessons != null){
                holder.text2.setText(lessons.getTitle());
            }
            return convertView;
        }

        class ViewHolder
        {
            public TextView text2;
        }
    }

    private void disableAutoScrollToBottom() {
        sv_include.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        sv_include.setFocusable(true);
        sv_include.setFocusableInTouchMode(true);
        sv_include.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.requestFocusFromTouch();
                return false;
            }
        });
    }


}
